#include <hidef.h>      /* common defines and macros */
#include "derivative.h"      /* derivative-specific definitions */
#include "FSM-OO-test.h"
#include "log.h"
#include "timer.h"
#include "irq.h"

/* handler */
void self_check_enter(void)
{
  log_debug("Enter: self_check_enter\n");
}
void self_check_running(void)
{
  log_debug("self_check_running ...\n");
  delayms(1000);
  
}
void self_check_exit(void)
{
  log_debug("Exit: self_check_exit\n");
}

void idle_running(void)
{
  log_debug("idle_running ...\n");
  delayms(1000);
}


static fsm_state_t self_check_state = {self_check_enter, self_check_running, self_check_exit};
static fsm_state_t idle_state = {NULL, idle_running, NULL};
static fsm_state_t handle_uart_data_state = {NULL, NULL, NULL};
static fsm_state_t scan_node_state = {NULL, NULL, NULL};
static fsm_state_t erase_flash_state = {NULL, NULL, NULL};
static fsm_state_t write_flash_conf_state = {NULL, NULL, NULL};
static fsm_state_t download_flash_state = {NULL, NULL, NULL};

static fsm_t fsm_dev;    


void run_test_fsm(void)
{
  register_key_irq();
  
  fsm_init(&fsm_dev, &self_check_state);
       

  while(1)
  {
    fsm_running(&fsm_dev);  
  }
}


void portH3_key1_handler(void *p_parameter)
{
  log_debug("key1 press on!\n");
  transfer_state(&fsm_dev, &idle_state);
}
void portH2_key2_handler(void *p_parameter)
{
  log_debug("key2 press on!\n");
  transfer_state(&fsm_dev, &self_check_state);
}
void portH1_key3_handler(void *p_parameter)
{
  log_debug("key3 press on!\n");
}
void portH0_key4_handler(void *p_parameter)
{
  log_debug("key4 press on!\n");
}

void register_key_irq(void)
{
  gpio_irq_info_t irq_info3 = {GPIO_IRQ_H, 3, LOW_EDGE_TRIGGER};
  gpio_irq_info_t irq_info2 = {GPIO_IRQ_H, 2, LOW_EDGE_TRIGGER};
  gpio_irq_info_t irq_info1 = {GPIO_IRQ_H, 1, LOW_EDGE_TRIGGER};
  gpio_irq_info_t irq_info0 = {GPIO_IRQ_H, 0, LOW_EDGE_TRIGGER};
  request_gpio_irq(&irq_info3, portH3_key1_handler, NULL);
  request_gpio_irq(&irq_info2, portH2_key2_handler, NULL);
  request_gpio_irq(&irq_info1, portH1_key3_handler, NULL);
  request_gpio_irq(&irq_info0, portH0_key4_handler, NULL);
}




